﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;

public partial class SampleKeptLog : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private DaSamplekeptlog dalKeptlog = new DaSamplekeptlog();
    private DaSampleKept dalKept = new DaSampleKept();

    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            AspNetPager.PageSize = 15;

            this.BtnQuery_Click(null, null);

            this.SetPanelVisiable(SummaryPanel);

            DataSet ds = dalKeptlog.GetDistinctSampleName();
            Utility.BindDataSet2Combox(ds, DdlSample, "sample_name", "sam_kep_id");

            if (!string.IsNullOrEmpty(Request.QueryString["samplekeptid"]))
            {
                string strSamKeptID = Request.QueryString["samplekeptid"].ToString();
                this.SetPanelVisiable(CreatePanel);
                DdlSample.SelectedValue = strSamKeptID;
                DdlSample.Enabled = false;

                MoSampleKept mod = dalKept.GetEntities(" where Sam_Kep_ID=" + strSamKeptID + "")[0];
                if (mod != null)
                {
                    CreateSender.Text = mod.Receiver;
                    CreateSender.Enabled = false;
                }
            }
        }
    }

    protected void CreateSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        MoSamplekeptlog mod = new MoSamplekeptlog();

        try
        {
            mod.Keptdate = DateTime.Now;
            mod.Receiver = CreateReceiver.Text.Trim();
            mod.Rem = CreateRem.Text.Trim();
            mod.Sam_Kep_Id = DdlSample.SelectedValue.ToString();
            mod.Sender = CreateSender.Text.Trim();
            mod.Status = "在管";
            mod.Storeaddr = CreateStoreAddr.Text.Trim();

            int rtn = dalKeptlog.AddExchange(mod);

            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "样品交接信息保存成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "样品交接信息保存失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "样品交接信息保存失败！"; divResult.Visible = true;
        }
    }

    protected void CreateBack_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["samplekeptid"]))
        {
            Response.Redirect("SampleKept.aspx");
        }
        else
        { 
            this.SetPanelVisiable(SummaryPanel);
        }
    }

    protected void CreateBack_Click1(object sender, EventArgs e)
    {
        Response.Redirect("SampleKept.aspx");
    }

    protected void CreateGo_Click(object sender, EventArgs e)
    {
        this.CreateCancel();
        this.SetPanelVisiable(CreatePanel);
    }

    protected void DetailBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void GvDetail_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();

        this.DisplayDetailInfo(strKey);
        this.SetPanelVisiable(DetailPanel);
    }

    protected void SetPanelVisiable(Panel panel)
    {
        CreatePanel.Visible = false;
        DetailPanel.Visible = false;
        SummaryPanel.Visible = false;

        panel.Visible = true;
    }

    protected void BtnQuery_Click(object sender, EventArgs e)
    {
        //这里查询信息并绑定到gridview上。
        string strCondition = " where 1=1 ";

        int pagecount = 0;
        if (sender != null && e != null)
        {
            AspNetPager.CurrentPageIndex = 0;
        }
        if (!string.IsNullOrEmpty(TxtSample_ID.Text.Trim()))
        {
            strCondition = strCondition + "and  Sam_Kep_ID='" + TxtSample_ID.Text.Trim() + "'";
        }
        if (!string.IsNullOrEmpty(TxtSampleName.Text.Trim()))
        {
            strCondition = strCondition + "and  Sample_Name like '%" + TxtSampleName.Text.Trim() + "%'";
        }

        this.GridView.DataSource = dalKeptlog.GetSampleKeptLogRecords(strCondition, AspNetPager.PageSize, AspNetPager.CurrentPageIndex - 1, out pagecount);
        GridView.DataBind();
        AspNetPager.RecordCount = pagecount;
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void DisplayDetailInfo(string strKey)
    {

        //这里查询员工信息
        DataSet ds = dalKeptlog.GetSampleKeptLog(strKey);

        if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
        {
            //然后给查看视图赋值。
            DetailID.Text = ds.Tables[0].Rows[0]["id"].ToString();
            DetailKeptDate.Text = ds.Tables[0].Rows[0]["KeptDate"].ToString();
            DetailEndDate.Text = ds.Tables[0].Rows[0]["EndDate"].ToString();
            DetailReceiver.Text = ds.Tables[0].Rows[0]["Receiver"].ToString();
            DetailRem.Text = ds.Tables[0].Rows[0]["Rem"].ToString();
            DetailSample_Name.Text = ds.Tables[0].Rows[0]["Sample_Name"].ToString();
            DetailSender.Text = ds.Tables[0].Rows[0]["Sender"].ToString();
            DetailStatus.Text = ds.Tables[0].Rows[0]["status"].ToString();
            DetailStoreAddr.Text = ds.Tables[0].Rows[0]["StoreAddr"].ToString();
        }
    }

    protected void CreateCancel()
    {
        CreateID.Text = "";
        CreateReceiver.Text = "";
        CreateRem.Text = "";
        CreateSender.Text = "";
        CreateStoreAddr.Text = "";
    }

    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {
        this.BtnQuery_Click(null, null);
    }

}
